<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分类页</title>
    <link rel="stylesheet" href="./css/style.css">
</head>

<body>
    <div class="fix-title">
        <div class="back">&lt;</div>
        <span>商品列表</span>
        <div class="text"><a href="search.html">搜索</a></div>
    </div>
    <div class="cate-list-fix">
        <ul>
            <!-- <li class="active"><a href="">分类1</a></li>
            <li><a href=""><a href="">分类2</a></li>
            <li><a href=""><a href="">分类3</a></li>
            <li><a href=""><a href="">分类4</a></li>
            <li><a href=""><a href="">分类5</a></li> -->
        </ul>
    </div>
    <div class="good-list">
        <ul>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
        </ul>
        <div class="empty">
            <span>空空如也...</span>
        </div>
        <p class="desc">数据正在加载中...</p>
        <p class="desc">我也是有底线的...</p>
    </div>
</body>
<script>

    // const xhr = new XMLHttpRequest()
    // xhr.open('get', 'http://124.223.14.236:3001/api/goods/cate')
    // xhr.send()
    // xhr.onreadystatechange = () => {
    //     if (xhr.readyState === 4 && xhr.status === 200) {
    //         console.log(JSON.parse(xhr.responseText));
    //         const str = JSON.parse(xhr.responseText).data.map(item => {
    //             const { id, name } = item
    //             return `<li><a href="list.html?id=${id}">${name}</a></li>`
    //         }).join('')
    //         document.querySelector('.cate-list-fix').innerHTML = str


    //         const xhr1 = new XMLHttpRequest()
    //         xhr1.open('get', `http://124.223.14.236:3001/api/goods?cate_id=${location.search.substring(4)}`)
    //         xhr1.send()
    //         xhr1.onreadystatechange = () => {
    //             if (xhr1.readyState === 4 && xhr1.status === 200) {
    //                 console.log(JSON.parse(xhr1.responseText));
    //                 const str1 = JSON.parse(xhr1.responseText).data.list.map(item => {
    //                     const { id, cate_id, img, price, title } = item
    //                     return `
    //                             <li>
    //                                 <img src="${img}" alt="">
    //                                 <h3>${title}</h3>
    //                                 <p>
    //                                     <span>￥${price.toFixed(2)}</span>
    //                                     <span>+</span>
    //                                 </p>
    //                             </li>
    //                             `
    //                 }).join('')
    //                 document.querySelector('.good-list ul').innerHTML = str1
    //             }
    //         }
    //     }
    // }



    const three = new XMLHttpRequest()
    three.open('get', `http://124.223.14.236:3001/api/goods?cate_id=${location.search.substring(4)}`)
    three.send()
    three.onreadystatechange = () => {
        if (three.readyState === 4 && three.status === 200) {
            console.log(JSON.parse(three.responseText).data.list);
            const ga = JSON.parse(three.responseText).data.list.map(function (item) {
                return `
                                <li>
                                <img src="${item.img}" alt="">
                                <h3>${item.title}</h3>
                                <p>
                                    <span>${item.price}yuan</span>
                                    <span>+</span>
                                </p>
                                </li>
                                `
            }).join('')
            document.querySelector('.good-list ul').innerHTML = ga

        }

    }




    const xhr = new XMLHttpRequest()
    xhr.open('get', 'http://124.223.14.236:3001/api/goods/cate')
    xhr.send()
    xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status === 200) {
            // console.log(JSON.parse(xhr.responseText));

            const one = JSON.parse(xhr.responseText).data.map(function (item) {
                return `<li><a href="javascript:;" data-id="${item.id}">${item.name}</a></li>`
            }).join('')
            document.querySelector('.cate-list-fix').innerHTML = one

            document.querySelector('.cate-list-fix').addEventListener('click', function (e) {
                if (e.target.tagName === 'A') {

                    // console.log();
                    const xhr1 = new XMLHttpRequest()
                    xhr1.open('get', `http://124.223.14.236:3001/api/goods?cate_id=${e.target.dataset.id}`)
                    xhr1.send()
                    xhr1.onreadystatechange = () => {
                        if (xhr1.readyState === 4 && xhr1.status === 200) {
                            // console.log(JSON.parse(xhr1.responseText));
                            const two = JSON.parse(xhr1.responseText).data.list.map(function (item) {
                                // console.log(item);
                                return `
                                <li>
                                <img src="${item.img}" alt="">
                                <h3>${item.title}</h3>
                                <p>
                                    <span>${item.price}yuan</span>
                                    <span>+</span>
                                </p>
                                </li>
                                `

                            }).join('')

                            document.querySelector('.good-list ul').innerHTML = two

                        }

                    }

                }


            })


        }

    }















</script>

</html>